package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.PaymentOrder;
import com.ecoolex.chinadutyfree.retailer.entity.PaymentOrderRecord;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryPaymentOrderDTO;
import com.ecoolex.chinadutyfree.retailer.ws.PaymentOrderService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.Admin, description = "支付订单")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = PaymentOrderGraphql.AUTH, name = "支付订单查询")
public class PaymentOrderGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-PAYMENT-ORDER";

	@Reference
	private PaymentOrderService service;

	@Autowired
	private ConsumerMemberManager consumerMemberManager;

	@TypeField(type = PaymentOrder.class, field = "user", description = "用户信息")
	public ConsumerMember user(@Source PaymentOrder source) {
		return consumerMemberManager.getByUserId(source.getUserId());
	}

	@TypeField(type = PaymentOrder.class, field = "recordList", description = "订单记录列表")
	public List<PaymentOrderRecord> recordList(@Source PaymentOrder source) {
		return service.queryRecordByOrderId(source.getPaymentOrderId());
	}

	private static final String AUTH_GET = AUTH + "-GET";

	@QueryField(field = "paymentOrder", description = "获取支付订单详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取支付订单详情")
	public PaymentOrder get(@NonNull Integer paymentOrderId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getOrderById(paymentOrderId);
	}

	private static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "paymentOrderList", description = "查询支付订单列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询支付订单列表")
	public Pagination<PaymentOrder> query(@NonNull QueryPaymentOrderDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return service.queryOrderByQuery(query, sorts, page);
	}
}
